<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="600"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item field="group_id" label="角色组">
        <a-select v-model="formData.group_id"  placeholder="请选择" allow-clear>
              <a-option v-for="item of roleList"  :value="item.id" :label="item.name" />
        </a-select>
      </a-form-item>
      <a-form-item label="登录名称" field="name">
        <a-input v-model="formData.name" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="姓名" field="true_name">
        <a-input v-model="formData.true_name" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="Ta的登录密码" field="password">
        <a-input v-model="formData.password" :placeholder="mode=='add'?'请输入':'不修改请不要填写'" />
      </a-form-item>
      <a-form-item label="Ta的操作密码" field="pay_password">
        <a-input v-model="formData.pay_password" :placeholder="mode=='add'?'请输入':'不修改请不要填写'" />
      </a-form-item>
      <a-form-item label="状态" field="state">
        <a-space size="large">
          <a-radio-group v-model="formData.state" :default-value="0" size="small">
            <a-radio v-for="item of ENUMS.STATE" :value="item.value">{{ item.title }}</a-radio>
          </a-radio-group>
        </a-space>
      </a-form-item>
      <a-form-item label="操作密码" field="admin_pay_password">
        <a-input-password v-model="formData.admin_pay_password" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="谷歌验证码" field="google_code">
        <a-input v-model="formData.google_code" placeholder="请输入" />
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed,watch } from 'vue'
import { Message } from '@arco-design/web-vue'
import systemApi from '@/api/system/system'
import ENUMS  from '@/utils/enums'
import {throttle} from  'lodash'
const roleList =ref([])
// 获取角色组列表
const getParams =async()=>{
const {data} =await systemApi.getUserParams()
  roleList.value= data.group
}
const emit = defineEmits(['success'])
console.log('ENUMS',ENUMS.STATE);
// 引用定义
const formRef = ref()
const mode = ref('')
const visible = ref(false)
const loading = ref(false)
const roleData = ref([])

let title = computed(() => {
  return '管理员' + (mode.value == 'add' ? '-新增' : '-编辑')
})
watch(mode, (newValue) => {
  rules.value.password[0].required = newValue === 'add';
  rules.value.pay_password[0].required = newValue === 'add';
});

// 表单信息
const formData = reactive({
  group_id:undefined,
  name:'',
  true_name:'',
  password:'',
  pay_password:'',
  state:undefined,
  admin_pay_password:'',
  google_code:'',
  id:''
})

// 验证规则
const rules = ref({
  group_id: [{ required: true, message: '请选择' }],
  name: [{ required: true, message: '请输入' }],
  true_name: [{ required: true, message: '请输入' }],
  password: [{ required: false, message: '请输入' }], // 默认设置为 false
  pay_password: [{ required: false, message: '请输入' }], // 默认设置为 false
  admin_pay_password: [{ required: true, message: '请输入' }],
  google_code: [{ required: true, message: '请输入' }],
  state: [{ required: true, message: '请选择' }],
});
// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
  await getParams()
  if(type=='add'){
    formData.state=0
  }
}

// 初始化页面数据
const initPage = async () => {
}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
      if(key=='state'){
        formData[key] = Number(data[key])
      }else if(key == 'pay_password' || key == 'password'){
        formData[key] =''
      }
    }
  }
  console.log('formData',formData);
}

// 数据保存
const submit = throttle(async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    try{
      let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.id = undefined
      result = await systemApi.createUser(data)
    } else {
      // 修改数据
      result = await systemApi.updateUser(data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
    }catch{
      loading.value = false
    }
  }
  done(false)
},100)

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
